'''
json.loads():解析一个有效的JSON字符串并将其转换为Python对象(Python字典)
json.load():从一个文件读取JSON类型的数据,然后转转换成Python对象(Python字典)
json.dump:主要用来将python对象(字典)写入json文件
json.dumps:将python对象格式(字典)化成json字符
'''
import ujson
import os
    
cfgpath= "/cfg.json"
global cfg_data
    
def read_cfg_data():
    global cfg_data
    cfg_data = read_cfg()
    return cfg_data

def read_cfg():
    json_data={}
    with open(cfgpath,'r') as f:
        json_data=ujson.load(f)
    print("json_data",json_data)  
    return json_data            

def write_cfg(data):
    with open(cfgpath,'r') as fr:
        json_data = ujson.load(fr)
    #print("json_data----------------------------------------------------------",json_data) 
    # 云端写入配置参数
    if "netmode" in data:
        json_data["netmode"]=data["netmode"]
        print ("netmode",json_data["netmode"])
    if "nettype" in data:
        json_data["nettype"]=data["nettype"]
        print ("nettype",json_data["nettype"])

    if "hearttime" in data:
        json_data["hearttime"]=data["hearttime"]
        print ("hearttime",json_data["hearttime"])

    if "reporttime" in data:
        json_data["reporttime"]=data["reporttime"]
        print ("reporttime",json_data["reporttime"])

    if "resheart" in data:
        json_data["resheart"]=data["resheart"]
        print ("resheart",json_data["resheart"])

    if "senstype" in data:
        json_data["senstype"]=data["senstype"]
        print ("senstype",json_data["senstype"])

    if "sensmodel" in data:
        json_data["sensmodel"]=data["sensmodel"]
        print ("sensmodel",json_data["sensmodel"])
    
    if "wifi" in data:
        json_data["wifi"]["ssid"]=data["wifi"]["ssid"]
        json_data["wifi"]["pwd"]=data["wifi"]["pwd"]
        print ("wifi",json_data["wifi"])
    if "apn" in data:
        json_data["apn"]["apnid"]=data["apn"]["apnid"]
        json_data["apn"]["apnname"]=data["apn"]["apnname"]
        json_data["apn"]["apnpsw"]=data["apn"]["apnpsw"]
        print ("apn",json_data["apn"])

    if "serveraddr" in data:
        json_data["mqtt_lte"]["serveraddr"]=data["serveraddr"]
        print ("serveraddr",json_data["mqtt_lte"]["serveraddr"])
    if "serverport" in data:
        json_data["mqtt_lte"]["serverport"]=data["serverport"]
        print ("serverport",json_data["mqtt_lte"]["serverport"])
    if "qos1" in data:
        json_data["mqtt_lte"]["qos1"]=data["qos1"]
        print ("qos1",json_data["mqtt_lte"]["qos1"])
    #三元组
    if "deviceCode" in data:
        json_data["mqtt_lte"]["deviceCode"]=data["deviceCode"]
        print ("deviceCode",json_data["mqtt_lte"]["deviceCode"])
    if "deviceSecret" in data:
        json_data["mqtt_lte"]["deviceSecret"]=data["deviceSecret"]
        print ("deviceSecret",json_data["mqtt_lte"]["deviceSecret"])
    if "deviceKey" in data:
        json_data["mqtt_lte"]["deviceKey"]=data["deviceKey"]
        print ("deviceKey",json_data["mqtt_lte"]["deviceKey"])

    if "lan" in data:
        json_data["lan"]["ip"]=data["lan"]["ip"]
        json_data["lan"]["subnet"]=data["lan"]["subnet"]
        json_data["lan"]["gateway"]=data["lan"]["gateway"]
        json_data["lan"]["dns"]=data["lan"]["dns"]

    if "tcp" in data:
        json_data["tcp"]["host"]=data["tcp"]["host"]
        json_data["tcp"]["port"]=data["tcp"]["port"]

    #print (json_data)
    with open(cfgpath,'w') as fw:
        fw.write(ujson.dumps(json_data))


def copy_file(src_file, dest_file):
        # 打开源文件以读取
    try:
        with open(src_file, 'r') as src:
            # 打开目标文件以写入（如果文件已存在，这将会覆盖原有内容）
            with open(dest_file, 'w') as dst:
                # 逐行读取并写入到新文件
                for line in src:
                    dst.write(line)
        print(f"文件'{src_file}'已成功复制到'{dest_file}'。")
    except:
        print(f"错误：源文件'{src_file}'不存在。")


def read_file(path):
    result={}
    with open(path,'r') as f:
        data=ujson.load(f)
    # print("json_data",data)
    result = data
    print('!'*30)
    print(path,result)
